package io.github.cchenxi.crpc.core.consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import io.github.cchenxi.crpc.core.api.Loadbalancer;
import io.github.cchenxi.crpc.core.api.RegistryCenter;
import io.github.cchenxi.crpc.core.api.Router;
import io.github.cchenxi.crpc.core.cluster.RoundRibonLoadbalancer;
import io.github.cchenxi.crpc.core.meta.InstanceMeta;
import io.github.cchenxi.crpc.core.registry.ZkRegistryCenter;

/**
 * 消费者配置
 * Date: 2024-03-10
 *
 * @author chenxi
 */
@Configuration
public class ConsumerConfig {
    @Bean
    ConsumerBootstrap consumerBootstrap() {
        return new ConsumerBootstrap();
    }

    @Bean
    @Order(Integer.MIN_VALUE)
    public ApplicationRunner consumerRunner(@Autowired ConsumerBootstrap consumerBootstrap) {
        return x -> {
            System.out.println("ConsumerBootstrap starting...");
            consumerBootstrap.start();
            System.out.println("ConsumerBootstrap started...");
        };
    }

    @Bean
    public Router<InstanceMeta> router() {
        return Router.Default;
    }

    @Bean
    public Loadbalancer<InstanceMeta> loadBalancer() {
        return new RoundRibonLoadbalancer<>();
    }

    @Bean(initMethod = "start", destroyMethod = "stop")
    public RegistryCenter consumerRegistryCenter() {
        return new ZkRegistryCenter();
    }
}
